//
//  GuideViewManager.m
//  GuideViewDemo
//
//  Created by BaizYi on 2019/1/7.
//  Copyright © 2019 YJY. All rights reserved.
//

#import "GuideViewManager.h"


@interface GuideViewManager ()

@property(strong,nonatomic) UIView  *bgView;
@property(strong,nonatomic) UITapGestureRecognizer  *tap;
@property(assign,nonatomic) NSInteger tapNumber;
@property(strong,nonatomic) NSArray  *tapViews;
@property(strong,nonatomic) NSArray  *tips;


@end


@implementation GuideViewManager


+(void)showGuideViewWithTapViews:(NSArray<UIView *> *)tapViews tips:(NSArray<NSString *> *)tips{
    GuideViewManager *manager = [[GuideViewManager alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [manager showGuideViewWithTapViews:tapViews tips:tips];
}

-(void)showGuideViewWithTapViews:(NSArray<UIView *> *)tapViews tips:(NSArray<NSString *> *)tips{
    _tapViews = tapViews;
    _tips = tips;
    _tapNumber = 0;
    
    CGRect screenFrame = [UIScreen mainScreen].bounds;
    UIView *bgView = [[UIView alloc]initWithFrame:screenFrame];
    bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    self.bgView = bgView;
    [self addSubview:self.bgView];
    
    self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
    [self.bgView addGestureRecognizer:_tap];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [self addBezierPathWithFrame:screenFrame tapView:self.tapViews[self.tapNumber] tip:self.tips[self.tapNumber]];
}

-(void)addBezierPathWithFrame:(CGRect)screenFrame tapView:(UIView *)tapView tip:(NSString *)tip{
    
    CGRect tap_frame = [[tapView superview] convertRect:tapView.frame toView:self.bgView];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:screenFrame];
    
    [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(tap_frame.origin.x + tap_frame.size.width/2.0, tap_frame.origin.y + tap_frame.size.height/2.0) radius:tap_frame.size.width/2.0+10 startAngle:0 endAngle:2*M_PI clockwise:NO]];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    [self.bgView.layer setMask:shapeLayer];
    
    for (UIView *view in self.bgView.subviews) {
        if ([view isKindOfClass:[UIImageView class]] || [view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
    }
    
    CGFloat x = CGRectGetMidX(tap_frame);
    CGFloat y = CGRectGetMaxY(tap_frame);
    
    UIImage *guidImage = [UIImage imageNamed:@"guide3"];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x - 30, y+10, guidImage.size.width, guidImage.size.height)];
    imageView.image = guidImage;
    [self.bgView addSubview:imageView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
    label.font = [UIFont systemFontOfSize:14];
    label.text = tip;
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.bgView addSubview:label];
    
    
    NSLayoutConstraint *constrainx = nil;
    
    NSLayoutConstraint *constrainW = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:self.bgView.bounds.size.width/3];
    
    [label addConstraint:constrainW];
    //    [label addConstraint:constrainR];
    
    if (x < screenFrame.size.width / 3.0) {
        constrainx = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    }else if ((x > screenFrame.size.width / 3.0) &&(x <= screenFrame.size.width * 2 / 3.0))
    {
        //创建x居中的约束
        constrainx = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    }
    else
    {
        //创建x居右的约束
        constrainx = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    }
    //创建y坐标的约束
    NSLayoutConstraint * constrainty = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeBottom multiplier:1 constant:10];
    
    [self.bgView addConstraints:@[constrainx, constrainty]];
    _tapNumber ++;
    
}


-(void)onTap:(UITapGestureRecognizer *)tap{
    if (_tapNumber == self.tapViews.count) {
        [_bgView removeFromSuperview];
    }else{
        CGRect frame = [UIScreen mainScreen].bounds;
        [self addBezierPathWithFrame:frame tapView:self.tapViews[self.tapNumber] tip:self.tips[self.tapNumber]];
    }
    
}


@end
